package com.chkj.fbook.business.controller;

import com.chkj.fbook.common.utils.PageUtils;
import com.chkj.fbook.common.utils.Query;
import com.chkj.fbook.common.utils.R;
import com.chkj.fbook.business.domain.SeatTimeDO;
import com.chkj.fbook.business.service.SeatTimeService;
import com.chkj.fbook.common.annotation.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 桌位/座位预约时间表
 * 
 * @author shh_he
 * @email 980027164@qq.com
 * @date 2023-10-02 21:43:17
 */
 
@Controller
@RequestMapping("/business/seatTime")
public class SeatTimeController {

	@Autowired
	private SeatTimeService seatTimeService;
	
	@GetMapping()
	@Permission(name = "business:seatTime:seatTime")
	public String SeatTime(){
	    return "business/seatTime/seatTime";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@Permission(name = "business:seatTime:seatTime")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<SeatTimeDO> seatTimeList = seatTimeService.list(query);
		int total = seatTimeService.count(query);
		PageUtils pageUtils = new PageUtils(seatTimeList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@Permission(name = "business:seatTime:add")
	public String add(){
	    return "business/seatTime/add";
	}

	@GetMapping("/edit/{id}")
	@Permission(name = "business:seatTime:edit")
	public String edit(@PathVariable("id") String id,Model model){
		SeatTimeDO seatTime = seatTimeService.get(id);
		model.addAttribute("seatTime", seatTime);
	    return "business/seatTime/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@Permission(name = "business:seatTime:add")
	public R save(SeatTimeDO seatTime){
		
		if(seatTime.getStartTime()!=null&&seatTime.getEndTime()!=null) {
			seatTime.setTimeSlot(seatTime.getStartTimeStr()+"-"+seatTime.getEndTimeStr());
		}else {
			return R.error("开始时间或截止时间不能为空！");
		}
		
		if(seatTime.getSort()==null) {
			return R.error("排序号不能为空！");
		}
		
		if(seatTimeService.save(seatTime)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@Permission(name = "business:seatTime:edit")
	public R update( SeatTimeDO seatTime){
		
		if(seatTime.getSort()==null) {
			return R.error("排序号不能为空！");
		}
		
		seatTimeService.update(seatTime);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@Permission(name = "business:seatTime:remove")
	public R remove( String id){
		if(seatTimeService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@Permission(name = "business:seatTime:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		seatTimeService.batchRemove(ids);
		return R.ok();
	}
	
}
